const { Sequelize } = require("sequelize");
// const sequelize = require("@/db/seq");
const sequelize = require("@/db/seq");

const Album = sequelize.define("blog_album", {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: "相册名称",
  },
  cover: {
    type: Sequelize.STRING, // 图片地址
    comment: "相册封面",
  },
  categoryId: {
    type: Sequelize.INTEGER,
    allowNull: true,
    comment: "分类ID",
  },
  status: {
    type: Sequelize.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: "相册状态 (对应基础数据表 dictionary.type=album_status)", // 1: 正常, 0: 私密
  },
  userId: {
    type: Sequelize.INTEGER,
    allowNull: false,
    comment: "用户id",
  },
  description: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: "相册描述",
  },
  createdAt: {
    type: Sequelize.DATE,
    allowNull: false,
    defaultValue: Sequelize.NOW,
    comment: "创建时间",
  },
  updatedAt: {
    type: Sequelize.DATE,
    allowNull: false,
    defaultValue: Sequelize.NOW,
    comment: "更新时间",
  },
  deletedAt: {
    type: Sequelize.DATE,
    allowNull: true,
    comment: "删除时间",
  },
}, {
  // 默认情况下，sequelize 会将表名命名为复数，例如 User 会被映射为 users。
  // 如果你希望 sequelize 使用单数形式，可以将 freezeTableName 选项设置为 true。
  freezeTableName: true,
  // 默认情况下，sequelize 会将模型名称转换为复数形式，例如 User 会被映射为 users。 
})

// Album.sync({ 
//   force: false,
//   alter: true, // 是否自动更新表结构，true: 自动更新，false: 不自动更新
//  }) // 同步数据库

Album.associate = (models) => {
  Album.belongsTo(models.album_category, {
    foreignKey: 'categoryId',
    as: 'album_category'
  });
  Album.hasMany(models.blog_photo, {
    foreignKey: 'albumId',
    as: 'photos'
  });
};
Album.name = "Album";
module.exports = Album;